package com.ncr.travel.appmgr.client;

import com.google.gwt.animation.client.Animation;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Visibility;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.inject.Inject;
import com.ncr.nep.dashboardcore.rest.client.JSONCallback;
import com.ncr.travel.appmgr.client.command.GetProducts;
import com.ncr.travel.appmgr.client.model.ApplicationConfigurationFactory;
import com.ncr.travel.appmgr.client.mvp.EventBus;
import com.ncr.travel.appmgr.client.mvp.Presenter;
import com.ncr.travel.appmgr.client.mvp.View;
import com.ncr.travel.appmgr.client.presenter.MainWindowPresenterImpl;
import com.ncr.travel.appmgr.client.service.AppManagerWebService;
import com.ncr.travel.appmgr.model.shared.ApplicationConfiguration;

public class AppController implements ValueChangeHandler<String>, Presenter {
	private final EventBus eventBus;
	private final AppManagerWebService webService;
	private HasWidgets container;
	private final MainWindowPresenterImpl mainWindowPresenter;
    /**
     * Factory for screen config model objects.
     */
	private ApplicationConfigurationFactory applicationConfigurationFactory;
	
	@Inject
	public AppController(MainWindowPresenterImpl mainWindowPresenter, AppManagerWebService webService, ApplicationConfigurationFactory applicationConfigurationFactory, EventBus eventBus) {
		super();
		this.mainWindowPresenter = mainWindowPresenter;
		this.eventBus = eventBus;
		this.webService = webService;
        this.applicationConfigurationFactory = applicationConfigurationFactory;
		bind();
	}

	public void bind() {
		History.addValueChangeHandler(this);
	}

	public void go(final HasWidgets container) {
		this.container = container;

		if ("".equals(History.getToken())) {
			History.newItem("main");
		} else {
			History.fireCurrentHistoryState();
		}
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		String token = event.getValue();

		if (token != null) {
			if (token.equals("main")) {
				mainWindowPresenter.go(container);
			}
		}
	}

	@Override
	public void unbind() {
		// TODO Auto-generated method stub

	}

	@Override
	public View getView() {
		// TODO Auto-generated method stub
		return null;
	}

	public void loadApplicationConfiguration() {
		System.out.println("(AppController) Loading Product configuration...");
		new GetProducts(webService, new JSONCallback<ApplicationConfiguration>(applicationConfigurationFactory, ApplicationConfiguration.class, Constants.MOCK_SERVER_MODE) {
			
			@Override
			public void onSuccess(final ApplicationConfiguration result) {
				System.out.println("(MainWindowPresenter) Loaded Products.");
				GWT.runAsync(new RunAsyncCallback() {
					public void onFailure(Throwable caught) {
						Window.alert("Code download failed");
					}

					public void onSuccess() {
						mainWindowPresenter.populateProductsTabPanel(result);
						hideLoadingPopup();
					}
				});
			}

			@Override
			public void onFailure(Throwable throwable, int statusCode) {
				// TODO Auto-generated method stub
				hideLoadingPopup();
			}
		}).execute();
	}

	// Fade out the "Loading application" pop-up
	private void hideLoadingPopup() {
		final Element e = RootPanel.get("loading").getElement();

		Animation r = new Animation() {

			@Override
			protected void onUpdate(double progress) {
				e.getStyle().setOpacity(1.0 - progress);
			}

			@Override
			protected void onComplete() {
				e.getStyle().setVisibility(Visibility.HIDDEN);
			}

		};

		r.run(500);
	}

}
